<?php 
class CategoryController extends AuthController{
	//数据模型对象
	private $_db;
	private $_category;
	//构造函数
	public function __init(){
		parent::__init();
		$this->_db= K('Category');
		$this->_category=F("category");
	}
    public function index(){
		$this->category=$this->_category;
    	$this->display();
    }
    //添加栏目
    public function add(){
    	if(IS_POST){
    		if($this->_db->add_category()){
    			$this->success('栏目添加成功 ！','index');
    		}else{
    			$this->error($this->_db->error);
    		}
    	}else{
    		$this->display();
    	}
   	}
	public function edit(){
		if(IS_POST){
			if($this->_db->edit_category()){
				$this->success('栏目修改成功 ！','index');
			}else{
    			$this->error($this->_db->error);
    		}
		}else{
			$field=$this->_db->find(Q("cid"));
			$category = $this->_db->all();
			$category=Data::tree($category,'cname');
			foreach($category as $n=>$v){
				$v['selected']='';
				if($field['pid']==$v['cid']){
					$v['selected']='selected="selected"';
				}
				$v['disabled']='';
				if($field['cid']==$v['cid'] || Data::isChild($category,$v['cid'],$field['cid'])){
					$v['disabled']='disabled="disabled" class="disabled"';
				}
				$category[$n]=$v;
			}
			$this->field=$field;
			$this->category=$category;
			$this->display();
		}
	}
	public function del(){
		if($this->_db->del_category()){
			$this->success('栏目删除成功 ！','index');
		}else{
			$this->error($this->_db->error);
		}
	}
	public	function update_cache(){
		if($this->_db->update_cache()){
			$this->success('更新缓存成功！','index');
		}else{
			$this->error('缓存更新失败，请检查缓存目录'.CACHE_PATH.'写权限','index');
		}
	}
}
 ?>